let curPage = 1;
let pageSize = 3;
layui.use(['laypage', 'layer'], function(){
    page()
});

function findAll(obj) {
    let result = null;
    if(obj != null){
        curPage = obj.curr;
        pageSize = obj.limit;
    }
    $.ajax({
        url: 'http://localhost:8080/news',//地址：访问后台的地址
        data: {'pageNo' : curPage,
                'pageSize': pageSize},//前端传递给后台的数据
        type: 'get',
        async: false,
        success: function (data) {//后端给前端的一个反馈
            console.log(data)
            result=data
        }
    });
    // console.log(data)
    return result.data;
}
function page(){
    layui.use(['laypage', 'layer'], function(){
        let laypage = layui.laypage
            ,layer = layui.layer;
        laypage.render({
            contentType:"application/json;charset=utf-8"
            ,elem: 'page'
            ,count: findAll().total
            ,layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
            ,jump: function(obj){
                // console.log(findAll(obj).records)
                setList(findAll(obj).records)
            }
        });
    })
}
function setList(data)  {
    let html="";
    for (let i = 0; i < data.length; i++) {
        html += '<div>\n' +
            '                            <a href="javascript:void(0)" onclick="newInfo('+data[i].id+')">' +'['+data[i].type+'] '+data[i].name + '</a>\n' +
            '                            <p>' + data[i].createTime + '</p>\n' +
            '                        </div>';

    }
    $("#new_text").html(html);
}
function newInfo(id){
    sessionStorage.setItem("newsId",id)
    window.open("/html/newsInfo.html")
}